package com.example.springcloud.controller;

import com.example.springcloud.common.ResponseEntity;
import com.example.springcloud.common.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author 莫磊
 * @date 2025/8/21
 * @description
 */
@RestController
@RequestMapping("/consumer/user")
public class UserConsumerForRibbonController {

    @Resource
    private RestTemplate restTemplate;

    /**
     * 指定dept服务的服务名（spring.application.name）
     */
    private String serviceName = "USER-SERVICE";

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询用户", description = "返回指定id的用户", responses = {
            @ApiResponse(
                    description = "成功获取到部门信息", responseCode = "200",
                    content = @Content(schema = @Schema(implementation = User.class))
            )
    }
    )
    public ResponseEntity<User> getById(@PathVariable("id") Long id) {
        ResponseEntity forObject = restTemplate.getForObject("http://" + serviceName + "/user/getById/" + id, ResponseEntity.class);

        return forObject;
    }

    @PostMapping("/addUser")
    @Operation(summary = "添加用户")
    public ResponseEntity<User> addUser(User user) {
        ResponseEntity service = restTemplate.postForObject("http://" + serviceName + "/user/addUser", user, ResponseEntity.class);
        return service;
    }

    @Operation(summary = "根据用户名称模糊查询用户",
            parameters = @Parameter(name = "name", description = "用户名称"))
    @GetMapping("/findByNameLike/{name}")
    public ResponseEntity<List<User>> findByNameLike(@PathVariable("name") String name) {
        ResponseEntity forObject = restTemplate.getForObject("http://" + serviceName + "/user/findByNameLike/" + name, ResponseEntity.class);
        return forObject;
    }
}
